package com.situ.hotel.service.impl;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.hotel.entity.Order;
import com.situ.hotel.entity.Room;
import com.situ.hotel.mapper.RoomMapper;
import com.situ.hotel.service.RoomService;

@Service
public class RoomServiceImpl implements RoomService {

	@Resource
	private RoomMapper roomMapper;

	@Override
	public int add(Room room) {

		if (roomMapper.selectByNumber(room.getNumber()) != null) {
			System.out.println(room.getNumber());
			return -5;
		}
		// TODO Auto-generated method stub
		// 1.1 房间编号非空 不超过八个字符
		if (room.getNumber() == null || room.getNumber().length() == 0 || room.getNumber().length() > 8) {
			return -1;
		}
		if (room.getType() < 0 || room.getType() > 2) {
			return -2;
		}
		if (room.getDscp().length() > 128) {
			return -3;
		}
		if (room.getPrice().compareTo(new BigDecimal(0)) <= 0) {
			return -4;
		}
		// 1.2 房间类型 0 1 2
		// 1.3 描述不超过 128 字符
		// 1.4 价格 正数
		// 2. 保证房间号不能重复
		// 2.1 根据房间号查询数据库
		// 3.将房间信息保存到数据库.

		return roomMapper.insert(room);
	}

	@Override
	public PageInfo search(Integer page, Integer limit, Room room) {
		// TODO Auto-generated method stub
		if (page == null)
			page = 1;
		if (limit == null)
			limit = 10;
		PageHelper.startPage(page, limit); // 开启分页 执行sql 根据分页信息拼接limit
		List list = roomMapper.select(room);
		PageInfo pageInfo = new PageInfo<>(list);
		return pageInfo;
	}

	@Override
	public int edit(Room room) {

		// TODO Auto-generated method stub
		if (roomMapper.selectById(room.getId()) == null) {
			return -1;
		}
		Room sRoom = roomMapper.selectByNumber(room.getNumber());
		if (sRoom != null && sRoom.getId() != room.getId()) {
			return -2;
		}
		return roomMapper.update(room);
	}

	@Override
	public int remove(int id) {
		// TODO Auto-generated method stub
		Room room = roomMapper.selectById(id);
		room.setStatus(1);
		return roomMapper.update(room);
	}

	@Override
	public PageInfo search(Integer page, Integer limit, Room room, Order order) {
		// TODO Auto-generated method stub
		if (page == null)
			page = 1;
		if (limit == null)
			limit = 10;
		PageHelper.startPage(page, limit); // 开启分页 执行sql 根据分页信息拼接limit
		List list = roomMapper.selectByOrder(room, order);
		PageInfo pageInfo = new PageInfo<>(list);
		return pageInfo;
	}

//	@Override
//	public PageInfo search(Integer page, Integer limit, Room room, Order order) {
//		// TODO Auto-generated method stub
//		
//	}
}
